package Form;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

import Control.CommonControls;
import Control.Workshop_WorkshopMain_Controller;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.util.Calendar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class Workshop_WorkshopMain_Form extends JPanel{
	//Object Attributes
	private Main parser;
	private Workshop_WorkshopMain_Form panel;
	private Workshop_WorkshopMain_Controller control;
	private JDialog create;
	private JLabel lblTitle;
	private JPanel filterPanel;
	private JPanel searchPanel;
	private JLabel lblSearchBy;
	private JLabel lblSortBy;
	private JLabel lblWorkshopName;
	private JButton btnSearch;
	private JLabel lblCategory;
	private JComboBox category;
	private JLabel lblstartDate;
	private JSpinner startDate;
	private JRadioButton rdbtnWorkshopName;
	private JRadioButton rdbtnstartDate;
	private JRadioButton rdbtnStatus;
	private ButtonGroup sortGroup;
	private JRadioButton rdbtnAscending;
	private JRadioButton rdbtnDescending;
	private ButtonGroup orderGroup;
	private JScrollPane scrollPane;
	private JPanel headerPanel;
	private JPanel viewPanel;
	private JLabel lblWorkshopName_1;
	private JLabel lblstartDate_1;
	private JLabel lblStatus;
	private JSeparator separator;
	private JSeparator separator_1;
	private JTextField txtWorkshopName;
	private JButton btnCreateWorkshop;
	private JLabel lblBG;

	public Main getParser() {
		return parser;
	}

	public void setParser(Main parser) {
		this.parser = parser;
	}

	public Workshop_WorkshopMain_Controller getControl() {
		return control;
	}

	public void setControl(Workshop_WorkshopMain_Controller control) {
		this.control = control;
	}
	
	public Workshop_WorkshopMain_Form(Main parser){
		this.setParser(parser);
		panel = this;
		this.setControl(new Workshop_WorkshopMain_Controller(this));
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {30, 440, 145, 30};
		gridBagLayout.rowHeights = new int[] {30, 40, 50, 250, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 1.0, 0.0, 1.0};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 1.0};
		setLayout(gridBagLayout);
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.anchor = GridBagConstraints.SOUTHWEST;
		gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
		gbc_lblTitle.gridx = 1;
		gbc_lblTitle.gridy = 1;
		add(getLblTitle(), gbc_lblTitle);
		GridBagConstraints gbc_btnCreateWorkshop = new GridBagConstraints();
		gbc_btnCreateWorkshop.anchor = GridBagConstraints.SOUTH;
		gbc_btnCreateWorkshop.insets = new Insets(0, 0, 5, 5);
		gbc_btnCreateWorkshop.gridx = 2;
		gbc_btnCreateWorkshop.gridy = 1;
		add(getBtnCreateWorkshop(), gbc_btnCreateWorkshop);
		GridBagConstraints gbc_filterPanel = new GridBagConstraints();
		gbc_filterPanel.insets = new Insets(0, 0, 5, 5);
		gbc_filterPanel.fill = GridBagConstraints.BOTH;
		gbc_filterPanel.gridx = 1;
		gbc_filterPanel.gridy = 2;
		add(getFilterPanel(), gbc_filterPanel);
		GridBagConstraints gbc_searchPanel = new GridBagConstraints();
		gbc_searchPanel.gridheight = 2;
		gbc_searchPanel.insets = new Insets(0, 0, 5, 5);
		gbc_searchPanel.fill = GridBagConstraints.BOTH;
		gbc_searchPanel.gridx = 2;
		gbc_searchPanel.gridy = 2;
		add(getSearchPanel(), gbc_searchPanel);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 3;
		add(getScrollPane_1(), gbc_scrollPane);
		GridBagConstraints gbc_lblBG = new GridBagConstraints();
		gbc_lblBG.gridheight = 5;
		gbc_lblBG.gridx = 0;
		gbc_lblBG.gridwidth = 4;
		gbc_lblBG.gridy = 0;
		add(getLblBG(), gbc_lblBG);
	}
	

	public JLabel getLblTitle() {
		if (lblTitle == null) {
			lblTitle = new JLabel("Workshop");
			lblTitle.setForeground(Color.WHITE);
			lblTitle.setFont(new Font("Rockwell", Font.BOLD, 28));
			lblTitle.setBackground(new Color(0, 0, 0, 200));
			lblTitle.setOpaque(true);
		}
		return lblTitle;
	}
	public JPanel getFilterPanel() {
		if (filterPanel == null) {
			filterPanel = new JPanel();
			filterPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
			GridBagLayout gbl_filterPanel = new GridBagLayout();
			gbl_filterPanel.columnWidths = new int[]{0, 0, 0, 0, 0};
			gbl_filterPanel.rowHeights = new int[]{0, 0, 0};
			gbl_filterPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
			gbl_filterPanel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
			filterPanel.setLayout(gbl_filterPanel);
			GridBagConstraints gbc_lblSortBy = new GridBagConstraints();
			gbc_lblSortBy.insets = new Insets(0, 0, 5, 5);
			gbc_lblSortBy.gridx = 0;
			gbc_lblSortBy.gridy = 0;
			filterPanel.add(getLblSortBy(), gbc_lblSortBy);
			GridBagConstraints gbc_rdbtnWorkshopName = new GridBagConstraints();
			gbc_rdbtnWorkshopName.insets = new Insets(0, 0, 5, 5);
			gbc_rdbtnWorkshopName.gridx = 1;
			gbc_rdbtnWorkshopName.gridy = 0;
			filterPanel.add(getRdbtnWorkshopName(), gbc_rdbtnWorkshopName);
			GridBagConstraints gbc_rdbtnStartDate = new GridBagConstraints();
			gbc_rdbtnStartDate.insets = new Insets(0, 0, 5, 5);
			gbc_rdbtnStartDate.gridx = 3;
			gbc_rdbtnStartDate.gridy = 0;
			filterPanel.add(getRdbtnStartDate(), gbc_rdbtnStartDate);
			GridBagConstraints gbc_rdbtnStatus = new GridBagConstraints();
			gbc_rdbtnStatus.anchor = GridBagConstraints.WEST;
			gbc_rdbtnStatus.insets = new Insets(0, 0, 5, 0);
			gbc_rdbtnStatus.gridx = 2;
			gbc_rdbtnStatus.gridy = 0;
			filterPanel.add(getRdbtnStatus(), gbc_rdbtnStatus);
			getSortGroup().add(getRdbtnWorkshopName());
			getSortGroup().add(getRdbtnStartDate());
			getSortGroup().add(getRdbtnStatus());
			GridBagConstraints gbc_rdbtnAscending = new GridBagConstraints();
			gbc_rdbtnAscending.anchor = GridBagConstraints.WEST;
			gbc_rdbtnAscending.insets = new Insets(0, 0, 0, 5);
			gbc_rdbtnAscending.gridx = 1;
			gbc_rdbtnAscending.gridy = 1;
			filterPanel.add(getRdbtnAscending(), gbc_rdbtnAscending);
			GridBagConstraints gbc_rdbtnDescending = new GridBagConstraints();
			gbc_rdbtnDescending.anchor = GridBagConstraints.WEST;
			gbc_rdbtnDescending.insets = new Insets(0, 0, 0, 5);
			gbc_rdbtnDescending.gridx = 2;
			gbc_rdbtnDescending.gridy = 1;
			filterPanel.add(getRdbtnDescending(), gbc_rdbtnDescending);
			getOrderGroup().add(getRdbtnAscending());
			getOrderGroup().add(getRdbtnDescending());
		}
		return filterPanel;
	}
	public JPanel getSearchPanel() {
		if (searchPanel == null) {
			searchPanel = new JPanel();
			searchPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			GridBagLayout gbl_searchPanel = new GridBagLayout();
			gbl_searchPanel.columnWidths = new int[] {0, 130, -5};
			gbl_searchPanel.rowHeights = new int[] {30, 40, 30, 40, 30, 40, 30, 55, 0};
			gbl_searchPanel.columnWeights = new double[]{1.0, 1.0, 1.0};
			gbl_searchPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
			searchPanel.setLayout(gbl_searchPanel);
			GridBagConstraints gbc_lblSearchBy = new GridBagConstraints();
			gbc_lblSearchBy.anchor = GridBagConstraints.NORTHWEST;
			gbc_lblSearchBy.insets = new Insets(0, 0, 5, 5);
			gbc_lblSearchBy.gridx = 1;
			gbc_lblSearchBy.gridy = 0;
			searchPanel.add(getLblSearchBy(), gbc_lblSearchBy);
			GridBagConstraints gbc_lblCategory = new GridBagConstraints();
			gbc_lblCategory.anchor = GridBagConstraints.SOUTH;
			gbc_lblCategory.insets = new Insets(0, 0, 5, 5);
			gbc_lblCategory.gridx = 1;
			gbc_lblCategory.gridy = 1;
			searchPanel.add(getLblCategory(), gbc_lblCategory);
			GridBagConstraints gbc_category = new GridBagConstraints();
			gbc_category.insets = new Insets(0, 0, 5, 5);
			gbc_category.fill = GridBagConstraints.HORIZONTAL;
			gbc_category.gridx = 1;
			gbc_category.gridy = 2;
			searchPanel.add(getCategory(), gbc_category);
			GridBagConstraints gbc_lblWorkshopName = new GridBagConstraints();
			gbc_lblWorkshopName.anchor = GridBagConstraints.SOUTH;
			gbc_lblWorkshopName.insets = new Insets(0, 0, 5, 5);
			gbc_lblWorkshopName.gridx = 1;
			gbc_lblWorkshopName.gridy = 3;
			searchPanel.add(getLblWorkshopName(), gbc_lblWorkshopName);
			GridBagConstraints gbc_txtWorkshopName = new GridBagConstraints();
			gbc_txtWorkshopName.insets = new Insets(0, 0, 5, 5);
			gbc_txtWorkshopName.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtWorkshopName.gridx = 1;
			gbc_txtWorkshopName.gridy = 4;
			searchPanel.add(getTxtWorkshopName(), gbc_txtWorkshopName);
			GridBagConstraints gbc_lblstartDate = new GridBagConstraints();
			gbc_lblstartDate.anchor = GridBagConstraints.SOUTH;
			gbc_lblstartDate.insets = new Insets(0, 0, 5, 5);
			gbc_lblstartDate.gridx = 1;
			gbc_lblstartDate.gridy = 5;
			searchPanel.add(getLblstartDate(), gbc_lblstartDate);
			GridBagConstraints gbc_startDate = new GridBagConstraints();
			gbc_startDate.fill = GridBagConstraints.HORIZONTAL;
			gbc_startDate.insets = new Insets(0, 0, 5, 5);
			gbc_startDate.gridx = 1;
			gbc_startDate.gridy = 6;
			searchPanel.add(getstartDate(), gbc_startDate);
			GridBagConstraints gbc_btnSearch = new GridBagConstraints();
			gbc_btnSearch.anchor = GridBagConstraints.SOUTH;
			gbc_btnSearch.insets = new Insets(0, 0, 5, 5);
			gbc_btnSearch.gridx = 1;
			gbc_btnSearch.gridy = 7;
			searchPanel.add(getBtnSearch(), gbc_btnSearch);
		}
		return searchPanel;
	}
	public JLabel getLblSearchBy() {
		if (lblSearchBy == null) {
			lblSearchBy = new JLabel("Search by:");
			lblSearchBy.setFont(new Font("Tahoma", Font.PLAIN, 13));
		}
		return lblSearchBy;
	}
	public JLabel getLblSortBy() {
		if (lblSortBy == null) {
			lblSortBy = new JLabel("Sort by:");
			lblSortBy.setFont(new Font("Tahoma", Font.PLAIN, 13));
		}
		return lblSortBy;
	}
	public JLabel getLblWorkshopName() {
		if (lblWorkshopName == null) {
			lblWorkshopName = new JLabel("Workshop Name:");
		}
		return lblWorkshopName;
	}
	public JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					control.search();
				}
			});
		}
		return btnSearch;
	}
	public JLabel getLblCategory() {
		if (lblCategory == null) {
			lblCategory = new JLabel("Category:");
		}
		return lblCategory;
	}
	public JComboBox getCategory() {
		if (category == null) {
			category = new JComboBox();
		}
		return category;
	}
	public JLabel getLblstartDate() {
		if (lblstartDate == null) {
			lblstartDate = new JLabel("Start Date");
		}
		return lblstartDate;
	}
	public JSpinner getstartDate() {
		if (startDate == null) {
			startDate = new JSpinner();
			startDate.setModel(new SpinnerDateModel(CommonControls.getCurrentDate(), CommonControls.minusDayToDate(CommonControls.getCurrentDate(), 30), CommonControls.addMonthToDate(CommonControls.getCurrentDate(), 3), Calendar.DAY_OF_YEAR));
			startDate.setEditor(new JSpinner.DateEditor(startDate, "dd/MM/yyyy"));
		}
		return startDate;
	}
	public JRadioButton getRdbtnWorkshopName() {
		if (rdbtnWorkshopName == null) {
			rdbtnWorkshopName = new JRadioButton("Workshop Name");
			rdbtnWorkshopName.setSelected(true);
			rdbtnWorkshopName.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent arg0) {
					control.filterSort();
					control.generateList();
				}
			});
		}
		return rdbtnWorkshopName;
	}
	public JRadioButton getRdbtnStartDate() {
		if (rdbtnstartDate == null) {
			rdbtnstartDate = new JRadioButton("Start Date/Time");
			rdbtnstartDate.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					control.filterSort();
					control.generateList();
				}
			});
		}
		return rdbtnstartDate;
	}
	public JRadioButton getRdbtnStatus() {
		if (rdbtnStatus == null) {
			rdbtnStatus = new JRadioButton("Status");
			rdbtnStatus.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					control.filterSort();
					control.generateList();
				}
			});
		}
		return rdbtnStatus;
	}
	public ButtonGroup getSortGroup(){
		if(sortGroup == null) {
			sortGroup = new ButtonGroup();
		}
		return sortGroup;
	}
	public JRadioButton getRdbtnAscending() {
		if (rdbtnAscending == null) {
			rdbtnAscending = new JRadioButton("Ascending");
			rdbtnAscending.setSelected(true);
			rdbtnAscending.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					control.filterOrder();
					control.generateList();
				}
			});
		}
		return rdbtnAscending;
	}
	public JRadioButton getRdbtnDescending() {
		if (rdbtnDescending == null) {
			rdbtnDescending = new JRadioButton("Descending");
			rdbtnDescending.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					control.filterOrder();
					control.generateList();
				}
			});
		}
		return rdbtnDescending;
	}
	public ButtonGroup getOrderGroup(){
		if(orderGroup == null) {
			orderGroup = new ButtonGroup();
		}
		return orderGroup;
	}
	public JScrollPane getScrollPane_1() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setPreferredSize(new Dimension(440, 250));
			scrollPane.setColumnHeaderView(getHeaderPanel());
			scrollPane.setViewportView(getViewPanel());
		}
		return scrollPane;
	}
	public JPanel getHeaderPanel() {
		if (headerPanel == null) {
			headerPanel = new JPanel();
			headerPanel.setBackground(Color.BLACK);
			GridBagLayout gbl_headerPanel = new GridBagLayout();
			gbl_headerPanel.columnWidths = new int[] {180, 5, 75, 5, 150};
			gbl_headerPanel.rowHeights = new int[]{25, 0};
			gbl_headerPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0};
			gbl_headerPanel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
			headerPanel.setLayout(gbl_headerPanel);
			GridBagConstraints gbc_lblWorkshopName_1 = new GridBagConstraints();
			gbc_lblWorkshopName_1.insets = new Insets(0, 0, 0, 5);
			gbc_lblWorkshopName_1.gridx = 0;
			gbc_lblWorkshopName_1.gridy = 0;
			headerPanel.add(getLblWorkshopName_1(), gbc_lblWorkshopName_1);
			GridBagConstraints gbc_separator = new GridBagConstraints();
			gbc_separator.fill = GridBagConstraints.BOTH;
			gbc_separator.insets = new Insets(0, 0, 0, 5);
			gbc_separator.gridx = 1;
			gbc_separator.gridy = 0;
			headerPanel.add(getSeparator(), gbc_separator);
			GridBagConstraints gbc_lblStatus = new GridBagConstraints();
			gbc_lblStatus.insets = new Insets(0, 0, 0, 5);
			gbc_lblStatus.gridx = 2;
			gbc_lblStatus.gridy = 0;
			headerPanel.add(getLblStatus(), gbc_lblStatus);
			GridBagConstraints gbc_separator_1 = new GridBagConstraints();
			gbc_separator_1.fill = GridBagConstraints.BOTH;
			gbc_separator_1.insets = new Insets(0, 0, 0, 5);
			gbc_separator_1.gridx = 3;
			gbc_separator_1.gridy = 0;
			headerPanel.add(getSeparator_1(), gbc_separator_1);
			GridBagConstraints gbc_lblstartDate_1 = new GridBagConstraints();
			gbc_lblstartDate_1.insets = new Insets(0, 0, 0, 25);
			gbc_lblstartDate_1.gridx = 4;
			gbc_lblstartDate_1.gridy = 0;
			headerPanel.add(getLblstartDate_1(), gbc_lblstartDate_1);
		}
		return headerPanel;
	}
	public JPanel getViewPanel() {
		if (viewPanel == null) {
			viewPanel = new JPanel();
		}
		return viewPanel;
	}
	public JLabel getLblWorkshopName_1() {
		if (lblWorkshopName_1 == null) {
			lblWorkshopName_1 = new JLabel("Workshop Name");
			lblWorkshopName_1.setForeground(Color.WHITE);
		}
		return lblWorkshopName_1;
	}
	public JLabel getLblstartDate_1() {
		if (lblstartDate_1 == null) {
			lblstartDate_1 = new JLabel("Start Date/Time");
			lblstartDate_1.setForeground(Color.WHITE);
		}
		return lblstartDate_1;
	}
	public JLabel getLblStatus() {
		if (lblStatus == null) {
			lblStatus = new JLabel("Status");
			lblStatus.setForeground(Color.WHITE);
		}
		return lblStatus;
	}
	public JSeparator getSeparator() {
		if (separator == null) {
			separator = new JSeparator();
			separator.setOrientation(SwingConstants.VERTICAL);
			separator.setForeground(Color.WHITE);
		}
		return separator;
	}
	public JSeparator getSeparator_1() {
		if (separator_1 == null) {
			separator_1 = new JSeparator();
			separator_1.setOrientation(SwingConstants.VERTICAL);
			separator_1.setForeground(Color.WHITE);
		}
		return separator_1;
	}
	public JTextField getTxtWorkshopName() {
		if (txtWorkshopName == null) {
			txtWorkshopName = new JTextField();
			txtWorkshopName.setColumns(10);
		}
		return txtWorkshopName;
	}
	public JButton getBtnCreateWorkshop() {
		if (btnCreateWorkshop == null) {
			btnCreateWorkshop = new JButton("Create Workshop");
			btnCreateWorkshop.setVisible(false);
			
			if(CommonControls.getUserType().equals("Staff")){
				getBtnCreateWorkshop().setVisible(true);
			}
			
			btnCreateWorkshop.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					create = new JDialog(parser, true);
					create.getContentPane().add(new Workshop_CreateWorkshop_Form(panel));
					create.pack();
					create.setLocationRelativeTo(parser);
					create.setVisible(true);					
				}
			});
		}
		return btnCreateWorkshop;
	}

	public JDialog getCreate() {
		return create;
	}

	public Workshop_WorkshopMain_Form getPanel() {
		return panel;
	}
	public JLabel getLblBG() {
		if (lblBG == null) {
			lblBG = new JLabel("");
			lblBG.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage("Resources/Images/Workshop/main.jpg").getScaledInstance(610, 640, Image.SCALE_DEFAULT)));		
		}
		return lblBG;
	}
}
